//
//  WeatherAddViewController.m
//  ToWhere
//
//  Created by dllo on 15/11/16.
//  Copyright © 2015年 www.TE.com. All rights reserved.
//

#import "WeatherAddViewController.h"
#import "CoreDatamanager.h"
#import "Citys.h"
#import "WeatherAddCollectionViewCell.h"
#import "KongWAreaViewController.h"
@interface WeatherAddViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, UIGestureRecognizerDelegate>

@property (nonatomic, retain) NSMutableArray *array;
@property (nonatomic, retain) UICollectionView *collectionView;
@property (nonatomic, retain) CoreDatamanager *coreDatamanager;
//cell的删除按钮
@property (nonatomic, retain) UIButton *btn;
@end

@implementation WeatherAddViewController

- (void)dealloc
{
    [_weather release];
    [_array release];
    [_collectionView release];
    [_coreDatamanager release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //设置返回按钮
    UIButton *turnBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    turnBtn.frame = CGRectMake(0  , 0, 40 *kWidth, 40 *kHeight);
    [turnBtn setImage:[UIImage imageNamed:@"ms_back@2x"] forState:UIControlStateNormal];
    [turnBtn addTarget:self action:@selector(turnAction) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:turnBtn];
    self.navigationController.navigationBar.barTintColor = [UIColor blackColor];
    self.view.backgroundColor = [UIColor whiteColor];
}

#pragma mark turn返回Action
- (void)turnAction{
    
    self.navigationController.navigationBar.hidden = YES;
    [self.navigationController popViewControllerAnimated:YES];
    
}

- (void)viewWillAppear:(BOOL)animated{
    self.title = @"天气";
    self.navigationController.navigationBarHidden = NO;
    self.navigationController.navigationBar.translucent = NO;
    [self.navigationController.navigationBar setTitleTextAttributes:
     @{NSFontAttributeName:[UIFont fontWithName:@"Helvetica-Bold" size:20 * kWidth],NSForegroundColorAttributeName:[UIColor colorWithRed:0.952 green:1.000 blue:0.967 alpha:1.000]}];
    
    self.array = [NSMutableArray array];
    self.coreDatamanager = [CoreDatamanager shareManager];
    
    NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Citys"];
    
    NSArray *resultArray = [_coreDatamanager.managedObjectContext executeFetchRequest:request error:nil];
    
    for (Citys *city in resultArray) {
        
        [self.array addObject:city.cityName];
    }
    UIImageView *image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"weather_lifeguide_pmDetail_bg.jpg"]];
    image.frame = CGRectMake(0, 0, 375 *kWidth, 667 *kHeight);
    [self.view addSubview:image];
   
    //创建UICOllectionView
    [self createCollectionView];
    
    //创建添加城市按钮
    [self createAddBtn];
    
    //创建刷新按钮
    [self createRefresh];

}
#pragma mark 创建添加城市按钮
- (void)createAddBtn{
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(10 * kWidth, (667 - 64 - 50) * kHeight - (1 - kHeight) * 50, 165 *kWidth, 40 *kHeight);
    [btn setTitle:@"添加城市" forState:UIControlStateNormal];
    btn.backgroundColor = [UIColor colorWithRed:0.447 green:0.643 blue:0.984 alpha:1];
    [btn addTarget:self action:@selector(addArea) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
}
- (void)addArea{

    KongWAreaViewController *kong = [[KongWAreaViewController alloc] init];
    self.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:kong animated:YES];
    self.hidesBottomBarWhenPushed = NO;
    [kong release];
}
#pragma mark 创建刷新按钮
- (void)createRefresh{

    UIButton *refresh = [UIButton buttonWithType:UIButtonTypeCustom];
    refresh.frame = CGRectMake(195 * kWidth, (667 - 114) * kHeight - (1 - kHeight) * 50, 165 *kWidth, 40 *kHeight);
    refresh.backgroundColor = [UIColor colorWithRed:1.000 green:0.678 blue:0.184 alpha:1];
    [refresh setTitle:@"刷新" forState:UIControlStateNormal];
    [refresh addTarget:self action:@selector(refresh) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:refresh];
}
- (void)refresh{

    [self viewWillAppear:YES];

}
- (void)createCollectionView{

    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    layout.itemSize = CGSizeMake(170 *kWidth, 180 *kHeight);
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0 , self.view.frame.size.width , (667 - 64 - 60) *kHeight) collectionViewLayout:layout];
    
    self.collectionView.dataSource = self;
    
    self.collectionView.delegate = self;
    
    self.collectionView.scrollEnabled = YES;
    
    [self.collectionView setBackgroundColor: [[UIColor whiteColor] colorWithAlphaComponent:0]];
    
    [self.collectionView registerClass:[WeatherAddCollectionViewCell class] forCellWithReuseIdentifier:@"WeatherAddCollectionViewCell"];
    
    [self.view addSubview:self.collectionView];
    
    [_collectionView release];
    
    [layout release];

}
#pragma mark collectionView回调方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return self.array.count;
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    
    return UIEdgeInsetsMake(10 *kWidth, 10 *kHeight, 10 *kWidth, 10 *kHeight);
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    
    return 1;
    
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    WeatherAddCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"WeatherAddCollectionViewCell" forIndexPath:indexPath];
    cell.city = self.array[indexPath.row];
    cell.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.5];
    //添加长按手势
    UILongPressGestureRecognizer *longRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handlelongPress:)];
    [cell addGestureRecognizer:longRecognizer];
    longRecognizer.minimumPressDuration = 1.0f;
    longRecognizer.delegate = self;
    longRecognizer.view.tag = indexPath.row;
    return cell;
}
- (void)handlelongPress:(UILongPressGestureRecognizer *)longRecognzier{

    if (longRecognzier.state == UIGestureRecognizerStateBegan) {
        UICollectionViewCell *cell = (UICollectionViewCell *)longRecognzier.view;
        self.btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.btn setImage:[UIImage imageNamed:@"weather_cha"] forState:UIControlStateNormal];
        self.btn.frame = CGRectMake(140 *kWidth, 10 *kHeight, 30 *kWidth, 30 *kWidth);
        self.btn.tag = longRecognzier.view.tag;
        [self.btn addTarget:self action:@selector(delegate:) forControlEvents:UIControlEventTouchUpInside];
        [cell addSubview:self.btn];

    }

}
- (void)delegate:(UIButton *)btn{

    NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Citys"];
    
    NSArray *resultArray = [_coreDatamanager.managedObjectContext executeFetchRequest:request error:nil];
    if (resultArray.count > 1){
        
        [_coreDatamanager.managedObjectContext deleteObject:resultArray[btn.tag]];
        [_coreDatamanager saveContext];
        
    }
    [btn removeFromSuperview];
    [self viewWillAppear:YES];
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{

    [self.delegate send:indexPath.row];
    self.navigationController.navigationBar.hidden = YES;
    [self.navigationController popViewControllerAnimated:YES];

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
